package jdbc;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class ContactsConnection {
    // 使用JNDI获取数据源并通过其获取数据库连接
    public static Connection open() throws NamingException, SQLException {
        InitialContext context = new InitialContext();
        DataSource source = (DataSource) context.lookup("java:/comp/env/jdbc/contacts");
        return source.getConnection();
    }
}
